package com.tsugun.mediaplayer.view.fragment;

import android.app.Fragment;
import android.content.DialogInterface;
import android.support.v4.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.BaseActivity;
import com.tsugun.mediaplayer.view.fragment.subfragment.SubBaseFragment;
import com.tsugun.mediaplayer.view.widget.MusicMenuDialog;

import java.util.List;

/**
 * BaseFragment
 * Created by shize on 2017/4/20.
 */

public abstract class BaseFragment extends Fragment {

    protected static final int MENU_ITEM_NEXT_PLAY = 0;
    protected static final int MENU_ITEM_COLLECTION = 1;
    protected static final int MENU_ITEM_ARTIST = 2;
    protected static final int MENU_ITEM_ALBUM = 3;
    protected static final int MENU_ITEM_INFO = 4;
    protected static final int MENU_ITEM_DELETE = 5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // 连接activity
    }

    @Override
    public void onStart() {
        super.onStart();
        setSettingTheme();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        // 分离activity
    }

    /**
     * 清除当前fragment中所有其他subFragment
     *
     * @param activity         当前所在的activity
     * @param subBaseFragments 需要清除的fragment集合
     */
    protected void clearFragments(BaseActivity activity, List<SubBaseFragment> subBaseFragments) {
        FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
        for (SubBaseFragment subBaseFragment : subBaseFragments) {
            transaction.remove(subBaseFragment);
        }
        transaction.commit();
    }

    /**
     * 设置主题
     */
    protected abstract void setSettingTheme();

    protected RecyclerView.OnChildAttachStateChangeListener getAttachListener() {
        return new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {
                ((BaseActivity) getActivity()).onSettingChanged();
            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {

            }
        };
    }

    /**
     * 设置主题色
     *
     * @param recyclerView 需要设置的list
     */
    protected void setSettingTheme(RecyclerView recyclerView) {
        recyclerView.setBackgroundColor(SettingPreferenceHelper.INSTANCE.getBACKGROUND_COLOR());
    }

    /**
     * 显示音乐信息弹窗
     *
     * @param musicModel 音乐
     */
    protected void showMusicInfoDialog(MusicModel musicModel, Context context) {
        MusicMenuDialog.BuilderInfoDialog builderInfoDialog = new MusicMenuDialog.BuilderInfoDialog(
                context, musicModel);
        MusicMenuDialog mDialog = builderInfoDialog.create();
        mDialog.show();
    }

    /**
     * 启动歌手界面
     */
    protected void startArtistFragment(String artist, BaseActivity baseActivity) {
        Bundle bundle = new Bundle();
        bundle.putString(ArtistFragment.INTENT_STRING_ARTIST_TITLE, artist);
        ArtistFragment fragment = new ArtistFragment();
        fragment.setArguments(bundle);
        baseActivity.getMusicFragmentManager().openFragment(fragment);
    }

    /**
     * 启动专辑界面
     */
    protected void startAlbumFragment(String album, BaseActivity baseActivity) {
        Bundle bundle = new Bundle();
        bundle.putString(AlbumFragment.INTENT_STRING_ALBUM_TITLE, album);
        AlbumFragment fragment = new AlbumFragment();
        fragment.setArguments(bundle);
        baseActivity.getMusicFragmentManager().openFragment(fragment);
    }

    /**
     * 显示删除确认会话框
     */
    protected void showDeleteAlertDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.DeleteAlertDialog);
        AlertDialog dialog = builder.setMessage(R.string.str_msg_delete_content)
                .setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton(R.string.str_delete, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        deleteMusic();
                        dialog.dismiss();
                    }
                }).create();
        dialog.show();
    }

    /**
     * 删除歌曲文件方法
     */
    protected void deleteMusic() { }
}
